package com.banmao.CarlCamp.Day05;

import lombok.extern.slf4j.Slf4j;

/**
 * https://leetcode.cn/problems/intersection-of-two-arrays/
 */
@Slf4j
public class LC349 {

    public int[] intersection(int[] nums1, int[] nums2) {
        // 数组长度规定小于1000，对于定长的题目仍可以用数组解法
        int[] arr = new int[1000];
        for (int i : nums1) {
            if (arr[i] > 0) {
                continue;
            }
            arr[i]++;
        }

        int count = 0;
        for (int i : nums2) {
            if (arr[i] > 0) {
                count++;
                arr[i] = -1;
            }
        }
        int[] res = new int[count];
        int index = 0;
        for (int i : nums2) {
            if (arr[i] == -1) {
                res[index++] = i;
                arr[i] = 1;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        LC349 lc349 = new LC349();
        int[] res = lc349.intersection(new int[]{1, 2, 2, 1}, new int[]{2, 2});
        log.info("{}", res);
    }

}
